<template>
  <div class="xq-form-wrapper p24">
    <!-------------------------------------------- 搜索区域start -------------------------------------------->
    <div class="flex-center-between mb24">
      <a-input-search v-model="productName" placeholder="企业服务名称" @change="onInputChange" style="width: 224px" />
      <a-button type="primary" @click="showCreateModal">添加企业服务</a-button>
    </div>
    <!-------------------------------------------- 搜索区域end -------------------------------------------->
    <!-------------------------------------------- 删除添加按钮区域start -------------------------------------------->
    <div class="xq-form-main">
      <!-------------------------------------------- 删除添加按钮区域end -------------------------------------------->
      <!-------------------------------------------- 表格区域区域start -------------------------------------------->
      <a-spin :spinning="spinning" class="xq-form-content">
        <XqTable
          :scroll="{ x: columns.length * 150 }"
          :pagination="pagination"
          :customize="FormJson['config']['table']['customize']"
          :dataSource="tableData"
          :columns="columns"
          :rowSelection="
            FormJson['config']['table']['extend']['batch']
              ? {
                  selectedRowKeys: selectedRowKeys,
                  onChange: onSelectChange
                }
              : null
          "
          class="xq-form-content-table"
          @change="onTableChange"
        >
          <!--------------------------------------------主要内容，表格，自定义 header -------------------------------------------->
          <template v-for="(col, index) in columns" #[`xq_${col.dataIndex}`]>
            <span :key="index">
              {{ col.customTitle }}
            </span>
          </template>
          <!-------------------------------------------- 主要内容，表格，自定义 td -------------------------------------------->
          <template v-for="(col, index) in columns" #[col.dataIndex]="text, record">
            <!-------------------------------------------- 主操作列区域start -------------------------------------------->
            <div v-if="col.dataIndex === 'productTag'" :key="index">
              <template v-if="text">
                <div v-for="li in text.split(',')" :key="li" class="tag">{{ li }}</div>
              </template>
            </div>
            <a
              v-else-if="col.dataIndex === 'productUrl'"
              :key="index"
              :href="text"
              target="_blank"
              class="ellipsis"
              style="width: 140px; display: block"
              >{{ text }}</a
            >
            <div v-else-if="col.dataIndex === 'productName'" :key="index" style="position: relative">
              {{ text }}
              <div v-if="record.isPrincipal" class="band-wrapper" />
            </div>
            <span v-else-if="col.dataIndex === 'tableAction'" :key="index">
              <a @click="showUpdateModal(record)">编辑</a>
              <a-divider type="vertical" />
              <a-popconfirm :title="`确认要删除记录吗？`" okText="确认" cancelText="取消" @confirm="deleteRecord(record)">
                <a>删除</a>
              </a-popconfirm>
            </span>
            <!-------------------------------------------- 主操作列区域end -------------------------------------------->
            <!-------------------------------------------- 枚举值转换start -------------------------------------------->
            <XqTableCell v-else :key="index" :record="record" :field="FormJson['fields'][col.dataIndex]" :dynamicData="dynamicData" />
            <!-------------------------------------------- 枚举值转换end -------------------------------------------->
          </template>
        </XqTable>
        <!-------------------------------------------- 分页区域start -------------------------------------------->
        <div class="xq-form-content-pagination">
          <div class="xq-form-content-pagination-total">共 {{ total }} 条</div>
          <a-pagination
            v-model="currentPage"
            :pageSize="pageSize"
            :total="total"
            :showSizeChanger="true"
            :showQuickJumper="true"
            :pageSizeOptions="['20', '50', '100']"
            @change="onPageChange"
            @showSizeChange="onPageSizeChange"
          />
        </div>
        <!-------------------------------------------- 分页区域区域end -------------------------------------------->
      </a-spin>
    </div>
    <!-------------------------------------------- 表格区域end -------------------------------------------->
    <!-------------------------------------------- 添加对话框区域start -------------------------------------------->
    <a-modal :pagination="false" v-model="createModalVisible" :maskClosable="false" :destroyOnClose="true" title="添加" :width="500">
      <XqFormBuild
        ref="createForm"
        :dataSource="dsForCreate"
        :dynamicData="dynamicData"
        :formFields="FormJson['fields']"
        :formValidators="FormJson['validators']"
        :formConfig="FormJson['config']['create']"
      />
      <XqModalFooter slot="footer" @cancel="createCancel" @ok="createOk" />
    </a-modal>
    <!-------------------------------------------- 添加对话框区域end -------------------------------------------->
    <!-------------------------------------------- 编辑对话框区域start -------------------------------------------->
    <a-modal v-model="updateModalVisible" :maskClosable="false" :destroyOnClose="true" title="编辑" :width="500">
      <XqFormBuild
        ref="updateForm"
        :dataSource="tempRecord"
        :dynamicData="dynamicData"
        :formFields="FormJson['fields']"
        :formValidators="FormJson['validators']"
        :formConfig="FormJson['config']['update']"
      />
      <XqModalFooter slot="footer" @cancel="updateCancel" @ok="updateOk" />
    </a-modal>
    <!-------------------------------------------- 编辑对话框区域end -------------------------------------------->
  </div>
</template>
<script>
import FormJson from "./FormJson"; // 引入FormJson数据
import XqCurdMixin from "xqjr-plugin-form/mixins/XqCurdMixin";
import { retrieveReq, createReq, deleteReq, deleteBatchReq, updateReq } from "./api"; // 引入接口文件

const curdMixin = new XqCurdMixin({
  createReq,
  retrieveReq,
  updateReq,
  deleteReq,
  deleteBatchReq,
  FormJson,
  tableConfigKey: "table"
});

export default {
  name: "",
  components: {},
  mixins: [curdMixin],
  props: {
    productTypeId: {
      type: String,
      default: "0"
    },
    productTypeIdList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      productName: "",
      pageSize: 20,
      dsForCreate: this.productTypeId === "0" ? {} : { productTypeId: this.productTypeId }
    };
  },
  computed: {
    retrieveParams() {
      return {
        request: {
          productTypeId: this.productTypeId
        }
      };
    },
    createParams() {
      return {};
    },
    updateParams() {
      return {};
    }
  },
  watch: {
    productTypeId(newVal) {
      this.retrieveRecord();
      if (newVal === "0") {
        this.dsForCreate = {};
      } else {
        this.dsForCreate = {
          productTypeId: newVal
        };
      }
    },
    productTypeIdList: {
      handler(newVal) {
        this.dynamicData = {
          ...this.dynamicData,
          productTypeIdList: newVal.map((item) => ({
            value: item.id,
            label: item.productTypeName
          }))
        };
        console.log(this.dynamicData);
      },
      deep: true
    }
  },
  created() {},
  mounted() {},
  methods: {
    onInputChange() {
      const params = {};
      if (this.productName) {
        params["productName"] = this.productName;
      }
      this.retrieveRecord(params);
    },
    buildDynamicDataCustom(tempDynamicData) {
      this.dynamicData = {
        ...tempDynamicData,
        productTypeIdList: this.productTypeIdList.map((item) => ({
          value: item.id,
          label: item.productTypeName
        }))
      };
    },
    formatValues(values) {
      values["productCover"] = this.formatUploadValue(values["productCover"]);
      return values;
    },
    formatDataSource(record) {
      return record;
    }
  }
};
</script>
<style lang="scss" scoped>
.band-wrapper {
  position: absolute;
  top: -20px;
  left: -16px;
  width: 50px;
  height: 50px;
  background-image: url("./tj.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
.tag {
  color: #fa8c16;
  background: #fff7e6;
  border: 1px solid #ffd591;
  padding: 0 7px;
  margin-right: 8px;
  line-height: 20px;
  font-size: 12px;
  display: inline-block;
  border-radius: 4px;
}
</style>
